<template>
  <div>
    <a-card>
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :xxl="6" :lg="8" :sm="8">
              <a-form-item label="名称">
                <a-input v-model="queryParam.name" placeholder="请输入名称" />
              </a-form-item>
            </a-col>

            <a-col :md="(!advanced && 8) || 24" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
              >
                <a-button
                  type="primary"
                  @click="searchha_datasource"
                  icon="search"
                  >查询</a-button
                >
                <a-button icon="reload" style="margin-left: 8px" @click="reQuery">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <div style="margin-bottom: 16px">
        <a-button
          type="primary"
          style="margin-left: 8px"
          @click="handleAdd"
          icon="plus"
          >添加数据源</a-button
        >
      </div>
      <a-table
        :scroll="{ x: 1500, y: 300 }"
        :columns="ucolumns"
        :data-source="udata"
        :pagination="upagination"
        :loading="loading"
        rowKey="id"
        @change="tableChange"
        size="middle"
        :bordered="ubordered"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ (upagination.current - 1) * upagination.pageSize + parseInt(index) + 1 }}
        </span>
        <span slot="name" slot-scope="text, record">
          {{ text }}
          <a-tag v-if="record.isdefault == 3200" color="cyan">默认</a-tag>
        </span>
        <span slot="operation" slot-scope="text, record">
          <template>
            <a @click="handleEdit(record, true)">详情</a>
            <a-divider type="vertical" />
          </template>
          <a-dropdown>
            <a class="ant-dropdown-link">更多<a-icon type="down" /> </a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleEdit(record, false)">编辑</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm
                  title="确定删除吗？"
                  ok-text="确定"
                  cancel-text="取消"
                  @confirm="delha_datasourceById(record)"
                >
                  <a-icon slot="icon" type="delete" />
                  <a href="javascript:;">删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </a-table>
    </a-card>
    <a-drawer
      :title="isView ? '数据源详情' : '数据源编辑'"
      :width="720"
      :visible="visible"
      :body-style="{ paddingBottom: '80px' }"
      @close="onClose"
      destroyOnClose
    >
      <edit ref="view" @close="onClose" @reloadList="searchha_datasource"></edit>
    </a-drawer>
  </div>
</template>

<style lang="less" src="@/style/base.less"></style>

<script>
import {
  delha_datasources,
  delha_datasource,
  queryha_datasourceList,
} from '@/api/workflow/ha_datasource'
import edit from '@/views/workflow/Act_dsn_form_datasourceEdit'

export default {
  data() {
    return {
      advanced: false, // 查询条件是否展开
      loading: false, // 遮罩是否显示
      visible: false, // 抽屉是否可见
      ubordered: false, // 表格边框是否显示
      isView: false, // 是否查看
      queryParam: {}, // 查询条件
      udata: [], // 表格数据
      upagination: {
        // 表格分页参数
        defaultPageSize: 20,
        total: 0,
        current: 1,
        pageSize: 20,
        showTotal: (total) => `共${total}条数据`,
        showSizeChanger: true,
        pageSizeOptions: ['10', '20', '50', '100'],
      },
      ucolumns: [
        {
          title: '序号',
          width: '50px',
          align: 'center',
          fixed: 'left',
          scopedSlots: { customRender: 'serial' },
        },
        {
          title: '名称',
          dataIndex: 'name',
          sorter: true,
          align: 'left',
          scopedSlots: { customRender: 'name' },
        },
        {
          title: '数据库类型',
          dataIndex: 'dbtype',
          sorter: true,
          align: 'left',
        },
        {
          title: '库名',
          dataIndex: 'schema',
          sorter: true,
          align: 'left',
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '150px',
          fixed: 'right',
          scopedSlots: { customRender: 'operation' },
        },
      ], // 表头
    }
  },
  created() {
    this.findha_datasourceList(this.queryParam)
  },
  methods: {
    /**
     *  查询表单所用的数据源列表
     */
    findha_datasourceList(queryParam) {
      const a = Date.now()
      //   console.log('queryParam', queryParam)
      queryha_datasourceList(queryParam).then((res) => {
        this.loading = true
        // console.log('queryha_datasourceList:', res.data)
        this.udata = res.data.content || []
        // 将当前页码和数量与后台绑定
        this.upagination.total = res.data.totalElements
        this.upagination.current = res.data.totalPages
        this.upagination.pageSize = res.data.size
        // 如果查询时间太短就给一个0.5秒的加载效果
        const b = Date.now()
        if (b - a < 500) {
          setTimeout(() => {
            this.loading = false
          }, 500)
        } else {
          this.loading = false
        }
      })
    },
    /**
     * 重置查询条件并查询数据
     */
    reQuery() {
      this.queryParam = {}
      this.findha_datasourceList(this.queryParam)
    },
    /**
     * 翻页、设置页数量时触发，将分页组建的当前页与大小传给后台
     */
    tableChange(pagination, filters, sorter, { currentDataSource }) {
      //   console.log('tableChange', pagination)
      //   console.log('filters', filters)
      //   console.log('sorter', sorter)
      //   console.log('{ currentDataSource }', { currentDataSource })
      this.queryParam.pageNumber = pagination.current
      this.queryParam.pageSize = pagination.pageSize
      if (sorter != null && sorter.order != null) {
        this.queryParam.sort = sorter.field
        this.queryParam.order = sorter.order.substring(0, sorter.order.length - 3)
      }
      this.findha_datasourceList(this.queryParam)
    },
    /**
     * 查询
     */
    searchha_datasource() {
      // 查询时将当前页码重置为1，否则会出现有数据但不显示的问题
      this.queryParam.pageNumber = 1
      // this.upagination.current = 1
      this.findha_datasourceList(this.queryParam)
    },
    /**
     * 选中行数据时触发
     */
    onSelectChange(selectedRowKeys) {
      //   console.log('selectedRowKeys changed: ', selectedRowKeys)
      this.selectedRowKeys = selectedRowKeys
    },
    /**
     * 打开抽屉
     */
    showDrawer() {
      this.visible = true
    },
    /**
     * 收回抽屉
     */
    onClose() {
      this.visible = false
    },
    /**
     * 进入新增页面
     */
    handleAdd() {
      this.showDrawer()
      this.$nextTick(() => {
        this.$refs.view.resetha_datasource()
      })
    },
    /**
     * 进入编辑页面
     */
    handleEdit(res, isView) {
      this.isView = isView
      //   console.log('view', res.id)
      this.showDrawer()
      this.$nextTick(() => {
        this.$refs.view.findha_datasourceById(res.id, isView)
      })
    },
    /**
     * 删除表单所用的数据源
     */
    delha_datasourceById(res) {
      delha_datasource(res.id).then((res) => {
        // console.log('delha_datasource:', res)
        if (res.code !== 0) {
          this.$message.success(res.msg)
        } else {
          this.selectedRowKeys = []
          this.searchha_datasource()
          this.$message.success('删除成功')
        }
      })
    },
    /**
     * 批量删除表单所用的数据源
     */
    delha_datasources() {
      delha_datasources(this.selectedRowKeys.toString()).then((res) => {
        // console.log('delha_datasources:', res)
        if (res.code !== 0) {
          this.$message.success(res.msg)
        } else {
          this.selectedRowKeys = []
          this.searchha_datasource()
          this.$message.success('删除成功')
        }
      })
    },
  },
  components: {
    edit,
  },
}
</script>
